package top.lyc829.iot_android.fragment.login;

import androidx.lifecycle.ViewModelProvider;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.xuexiang.xhttp2.XHttp;
import com.xuexiang.xhttp2.model.ApiResult;
import com.xuexiang.xhttp2.request.CustomRequest;
import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xpage.core.PageOption;
import com.xuexiang.xpage.enums.CoreAnim;
import com.xuexiang.xui.widget.actionbar.TitleBar;
import com.xuexiang.xui.widget.edittext.materialedittext.MaterialEditText;
import com.xuexiang.xui.widget.textview.supertextview.SuperButton;
import com.xuexiang.xutil.app.ActivityUtils;
import com.xuexiang.xutil.tip.ToastUtils;

import butterknife.BindView;
import top.lyc829.iot_android.MainActivity;
import top.lyc829.iot_android.R;
import top.lyc829.iot_android.core.BaseFragment;
import top.lyc829.iot_android.databinding.LoginFragmentBinding;
import top.lyc829.iot_android.entity.User;
import top.lyc829.iot_android.http.service.UserService;
import top.lyc829.iot_android.http.subscriber.TipProgressLoadingSubscriber;
import top.lyc829.iot_android.http.subscriber.TipRequestSubscriber;
import top.lyc829.iot_android.util.Utils;

@Page(anim = CoreAnim.none)
public class LoginFragment extends BaseFragment {

    @BindView(R.id.et_id)
    MaterialEditText etId;

    @BindView(R.id.et_password)
    MaterialEditText etPassword;

    @BindView(R.id.btn_login)
    SuperButton btnLogin;




    @Override
    protected TitleBar initTitle() {
        TitleBar titleBar = super.initTitle()
                .setTitle("登录")
                .disableLeftView();
        return titleBar;
    }

    public static LoginFragment newInstance() {
        return new LoginFragment();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.login_fragment;
    }

    @Override
    protected void initViews() {
        btnLogin.setOnClickListener(v -> {
            doLogin();
        });


    }

    private void doLogin() {

        if (etId.isEmpty() || etPassword.isEmpty()) {
            Utils.showToast("用户名或密码不能为空");
            return;
        }

        Integer id = Integer.parseInt(etId.getEditValue());
        String password = etPassword.getEditValue();

        CustomRequest request = XHttp.custom();
        request.call(request.create(UserService.class)
                .doLogin(new User(id, password)))
                .subscribeWith(new TipProgressLoadingSubscriber<ApiResult>(getProgressLoader()) {
                    @Override
                    protected void onSuccess(ApiResult apiResult) {
                        if (apiResult.isSuccess()) {
                            success();
                        } else {
                            Utils.showToast(apiResult.getMsg());
                        }

                    }
                });

    }

    private void success() {
        Utils.showToast("登录成功");
        popToBack();
        ActivityUtils.startActivity(MainActivity.class);
    }


}